package ltd.newbee.mall.controller.admin;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.NewbeeMallSupplierInfo;
import ltd.newbee.mall.service.ISupplierInfoService;
import ltd.newbee.mall.service.NewBeeMallUserService;
import ltd.newbee.mall.service.impl.SupplierInfoServiceImpl;
import ltd.newbee.mall.util.KeyUtil;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;


@Controller
@RequestMapping("/admin")
public class NewBeeMallSupplierController {

    @Resource
    private SupplierInfoServiceImpl supplierInfoService;

    @GetMapping("/suppliers")
    public String usersPage(HttpServletRequest request) {
        request.setAttribute("path", "suppliers");
        return "admin/newbee_mall_suppliers";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/suppliers/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty((CharSequence) params.get("page")) || StringUtils.isEmpty((CharSequence) params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        Integer page = Integer.parseInt(String.valueOf(params.get("page")));;
        //Integer limit = (Integer) params.get("limit");
        Integer limit = Integer.parseInt(String.valueOf(params.get("limit")));
        PageRequest pageRequest = PageRequest.of(page - 1, limit);

        return ResultGenerator.genSuccessResult(supplierInfoService.getNewBeeMallSupplierPage(pageRequest));
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/suppliers/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody NewbeeMallSupplierInfo newbeeMallSupplierInfo) {
        if (StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierName())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierAddress())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierPhone())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getContactName())) {
            return ResultGenerator.genFailResult("参数异常！");
        }


        newbeeMallSupplierInfo.setSupplierId(KeyUtil.genUniqueKey());

        String result = supplierInfoService.save(newbeeMallSupplierInfo);

        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/suppliers/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody  NewbeeMallSupplierInfo newbeeMallSupplierInfo) {
        if (StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierId())
                ||StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierName())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierAddress())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getSupplierPhone())
                || StringUtils.isEmpty(newbeeMallSupplierInfo.getContactName())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = supplierInfoService.modify(newbeeMallSupplierInfo);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/suppliers/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody String[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        try{
//            for (Integer supplierId : ids)
//            {
//                String id = String.valueOf(supplierId);
//                supplierInfoService.deleteById(id);
//            }
            for (String supplierId : ids)
            {
                supplierInfoService.deleteById(supplierId);
            }
            return ResultGenerator.genSuccessResult();

        }catch(Exception e)
        {
            return ResultGenerator.genFailResult("删除失败");
        }
    }


}
